package com.yijinsuo.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yijinsuo.R;
import com.yijinsuo.base.BaseActivity;
import com.yijinsuo.http.HttpCallBack;
import com.yijinsuo.http.HttpUtils;
import com.yijinsuo.mian.Config;
import com.yijinsuo.tool.MyCountTimer;
import com.yijinsuo.tool.VibratorUtil;
import com.yijinsuo.util.DES;
import com.yijinsuo.view.ClearEditText;
import com.yijinsuo.view.TitleBar;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 */
public class ForgetPasswordActivity extends BaseActivity {

    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.et_password_phone)
    ClearEditText etPasswordPhone;
    @Bind(R.id.et_new_password_forget)
    ClearEditText etNewPasswordForget;
    @Bind(R.id.et_new_password_forget_second)
    ClearEditText etNewPasswordForgetSecond;
    @Bind(R.id.tv_yanzhengma_message_check_code)
    ClearEditText tvYanzhengmaMessageCheckCode;
    @Bind(R.id.tv_yanzhengma_check_code)
    TextView tvYanzhengmaCheckCode;

    //
    private MyCountTimer myCountTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_forget_password);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {

        titlebar.setTitleBar(R.drawable.back_selector, "忘记密码", 0);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {

    }

    @OnClick({R.id.titlebar_iv_back, R.id.tv_yanzhengma_check_code, R.id.tv_password_yanzheng})
    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.titlebar_iv_back:

                finish();


                break;

            case R.id.tv_password_yanzheng:    //完成
                long[] pattern = { 100, 400, 100, 400 };

                String phone = etPasswordPhone.getText().toString().trim();
                String messageCode = tvYanzhengmaMessageCheckCode.getText().toString().trim();
                String newPassword = etNewPasswordForget.getText().toString().trim();
                String secondPassword = etNewPasswordForgetSecond.getText().toString().trim();  //确认密码

                if (TextUtils.isEmpty(phone)) {
                    etPasswordPhone.setShakeAnimation();
                    VibratorUtil.Vibrate(ForgetPasswordActivity.this,pattern,false);
                    Toast.makeText(ForgetPasswordActivity.this, " 手机号不能为空", Toast.LENGTH_SHORT).show();

                    return;
                } else if (TextUtils.isEmpty(messageCode)) {
                    VibratorUtil.Vibrate(ForgetPasswordActivity.this,pattern,false);
                    tvYanzhengmaMessageCheckCode.setShakeAnimation();
                    Toast.makeText(ForgetPasswordActivity.this, "验证码部门为空", Toast.LENGTH_SHORT).show();
                    return;

                } else if (TextUtils.isEmpty(newPassword)) {
                    VibratorUtil.Vibrate(ForgetPasswordActivity.this,pattern,false);
                    etNewPasswordForget.setShakeAnimation();
                    Toast.makeText(ForgetPasswordActivity.this, "新密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(secondPassword)) {
                    VibratorUtil.Vibrate(ForgetPasswordActivity.this,pattern,false);
                    etNewPasswordForgetSecond.setShakeAnimation();
                    Toast.makeText(ForgetPasswordActivity.this, "确认密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!newPassword.equals(secondPassword)) {
                    VibratorUtil.Vibrate(ForgetPasswordActivity.this,pattern,false);
                    etNewPasswordForgetSecond.setShakeAnimation();
                    etNewPasswordForget.setShakeAnimation();
                    Toast.makeText(ForgetPasswordActivity.this, "两次密码不匹配,请正确输入密码", Toast.LENGTH_SHORT).show();
                    return;

                }else{

                    CommitForgetPassword(phone,messageCode,newPassword,secondPassword);
                }


                break;


            case R.id.tv_yanzhengma_check_code:   //获取短信验证码

                if(!TextUtils.isEmpty(etPasswordPhone.getText().toString().trim())){
                    myCountTimer=new MyCountTimer(60000,1000,tvYanzhengmaCheckCode,R.string.txt_getMsgCode_validate);
                    myCountTimer.start();

                    getForpasswoedCode();

                }else {
                    Toast.makeText(ForgetPasswordActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                    etPasswordPhone.setShakeAnimation();
                }






                break;

            default:
        }
    }




    /**
     *
     * 获取忘记密码的验证码
     *
     */
    private void getForpasswoedCode() {
        HttpParams params=new HttpParams();
        params.put("userAccount",etPasswordPhone.getText().toString().trim());

        HttpUtils.post(Config.PASSWORDSEND, params, new HttpCallBack(ForgetPasswordActivity.this,"加载中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                Toast.makeText(ForgetPasswordActivity.this, message, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(ForgetPasswordActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });


    }


    /**
     *
     *  保存忘记密码
     * @param phone
     * @param messageCode
     * @param newPassword
     * @param secondPassword
     */

    private void CommitForgetPassword(String phone, String messageCode, String newPassword, String secondPassword) {


        try {
            HttpParams params=new HttpParams();
            params.put("userAccount",phone);
            params.put("userPassword", DES.encryptDES(newPassword,Config.DES_KEY));
            params.put("userPassword2",DES.encryptDES(secondPassword,Config.DES_KEY));
            params.put("phoneCode",DES.encryptDES(messageCode,Config.DES_KEY));

            HttpUtils.post(Config.RESETLOGINPASSWORD, params, new HttpCallBack(this,"保存中...") {
                @Override
                public void onSucceed(JSONObject result, String code, String message) {

                    Toast.makeText(ForgetPasswordActivity.this, message, Toast.LENGTH_SHORT).show();
                    finish();
                }

                @Override
                public void onError(String code, String message) {

                    Toast.makeText(ForgetPasswordActivity.this, message, Toast.LENGTH_SHORT).show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }





    }


}
